import {mat4, vec3} from "gl-matrix";
import {Camera} from "../../src/core/camera";



const screen_width = 800;
const screen_height = 600;
function screen_to_clip(x:number,y:number,z:number) {
    let cx = 2 * x / screen_width - 1;
    let cy = 1 - 2 * y / screen_height;
    return vec3.fromValues(cx,cy,z);
}

let x = screen_width / 2;
let y = screen_height / 2;

let near = screen_to_clip(x,y,-1);
let far = screen_to_clip(x,y,1);

console.log("near:",near);
console.log("far:",far);

// near 和 far 转换到 世界坐标系
let camera = new Camera(0,2,5);
camera.updateMatrix();

let invProjMatrix = mat4.create();
mat4.invert(invProjMatrix,camera.projMatrix);

let nearInView = vec3.create();
let farInView = vec3.create();
vec3.transformMat4(nearInView,near,invProjMatrix);
vec3.transformMat4(farInView,far,invProjMatrix);

console.log("nearInView:",nearInView);
console.log("farInView:",farInView);

let invViewMatrix = mat4.create();
mat4.invert(invViewMatrix,camera.viewMatrix);

let nearInWorld = vec3.create();
let farInWorld = vec3.create();
vec3.transformMat4(nearInWorld,nearInView,invViewMatrix);
vec3.transformMat4(farInWorld,farInView,invViewMatrix);

console.log("nearInWorld:",nearInWorld);
console.log("farInWorld:",farInWorld);


